#{extends 'main.html' /}
#{set title:'订单列表' /}
*{<script type="text/javascript" src="/public/sui/js/list.js"></script>}*
  <body>
    <div class="page-group">
      <div class="page">
        <header class="bar bar-nav">
             <a href="javascript:history.go(-1);" class="icon icon-left pull-left"></a>
             <h1 class="title">订单管理</h1>
        </header>
        <section class="content myorder scroll_y">
            <div class="buttons-tab" style="position:fixed; z-index:99; width:100%;">
                <a href="#" orderstatus="PAID" distributeStatus="UN_DISTRIBUTE" class="PAID UN_DISTRIBUTE  tab-link button">已支付</a>
                <a href="#"  orderstatus="DISTRIBUTIONING" distributeStatus="ROB_DISTRIBUTE" class=" DISTRIBUTIONING ROB_DISTRIBUTE tab-link button">配送中</a>
                <a href="#" orderstatus="DISTRIBUTION" distributeStatus="SEND_DISTRIBUTE" class=" DISTRIBUTION SEND_DISTRIBUTE tab-link button">已完成</a>
            </div>
            <div id="idOrderList" class="tabs" style=" margin-top:2rem;">
                </div>
        </section>
      #{include '/OrderCtrl/tempOrderList.html'/}

      #{include 'footer.html'/}
      </div>
    </div>

    <style>
        .pop_editbox{width: 100%; height:2rem; line-height: 1.8rem; color:#999; text-indent: 0.2rem; border-radius: 5px; border: 1px #eeeeee solid; margin: 0.5rem 0;}
        .myorder .card-header li{ height: auto;  overflow: hidden;}
    </style>
    <script>$.init();</script>
    <script>
        $(document).on('click','.openMoney', function () {
            var did = $(this).attr('did');
            $('#deliveryItem').val(did);
            $.popup('.open-about');
        });
        $(document).on('click','.popup-overlay', function () {
            $("#money").val('');
            $.closeModal('.open-about')
        });

        function confirm(){
            var money = $("#money").val();
            var deliveryItem = $('#deliveryItem').val();
            console.log("money",money);
            console.log("deliveryItem",deliveryItem);
            $.getJSON('/order/distributionOrder',{ deliveryId:deliveryItem,money:parseInt(money)},function (data) {
                console.log("data.success",data.success);
                if(data.success){
                    $.toast(data.msg)
                    $("#money").val('');
                    $('#deliveryItem').val('');
                    $.closeModal('.open-about')

                }else{
                    $.toast(data.msg)
                    $("#money").val('');
                    $('#deliveryItem').val('');
                    $.closeModal('.open-about')

                }
            })
            getOrderList(orderstatus,distributeStatus);
        }

        var orderstatus = 'PAID';
        var distributeStatus = 'UN_DISTRIBUTE';
        $(function () {
            getOrderList(orderstatus,distributeStatus);
            $(document).on('click', '.tab-link', function () {
                orderstatus = $(this).attr('orderstatus');
                distributeStatus = $(this).attr('distributeStatus');
                getOrderList(orderstatus,distributeStatus);
            });

        });
        function getOrderList(orderstatus,distributeStatus) {
            $('.tab-link').removeClass('active');
            $('.'+orderstatus).addClass('active');
            $.getJSON('/order/loadlist',{ orderstatus:orderstatus,distributeStatus:distributeStatus},function (data) {
                console.log(data);
                if(data.success){
                    var dataList = data.orderList;
                    var gettpl = document.getElementById('order_list_tpl').innerHTML;
                    laytpl(gettpl).render(dataList, function (html) {
                        document.getElementById('idOrderList').innerHTML = html;
                        $("#idOrderList").scrollTop(0);

                    });
                }else {
                    $.toast('喔喔~~ 数据加载不出来了')
                }
            })


        }


        function cancelStatus(itemId){
            $.confirm('您确认要取消订单吗?', function () {
                $.getJSON('/order/cancelItem',{ itemId:itemId},function (data) {
                    console.log("data.success",data.success);
                    if(data.success){
                        $.toast("订单取消成功")
                        getOrderList('PAID','UN_DISTRIBUTE');
                    }else {
                        $.toast(data.msg);
                        getOrderList('PAID','UN_DISTRIBUTE');
                    }
                })
            });
        }

        function runOwn(deliveryItem){
            $.getJSON('/order/distributionOwn',{ deliveryId:deliveryItem},function (data) {
                console.log("data.success",data.success);
                if(data.success){
                    $.toast("订单分配成功")
                    getOrderList('DISTRIBUTIONING','ROB_DISTRIBUTE');
                }else {
                    $.toast(data.msg)
                    getOrderList('PAID','UN_DISTRIBUTE');
                }
            })
        }

        function runOver(deliveryItem){
            $.getJSON('/order/disMerRunVoer',{ deliveryId:deliveryItem},function (data) {
                console.log("data.success",data.success);
                if(data.success){
                    $.toast("确认送达")
                    getOrderList('DISTRIBUTION','SEND_DISTRIBUTE');
                }else {
                    $.toast(data.msg)
                    getOrderList('DISTRIBUTIONING','ROB_DISTRIBUTE');
                }
            })
        }
    </script>
  </body>
